package com.emep.changzhi.analyse.service.equipment;

import com.emep.changzhi.analyse.entity.equipment.MoitorTypeInfo;
import com.emep.changzhi.analyse.repository.equipment.MoitorTypeInfoRepository;
import com.emep.changzhi.analyse.utils.MapUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;
import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.*;

/**
 * @author yangj
 * @version V1.0
 * @Description: 监测类型信息业务层
 * @date 2018/2/26  15:36
 */
@Slf4j
@Service
@Transactional
public class MoitorTypeInfoService {

    @Resource
    private MoitorTypeInfoRepository moitorSiteInfoRepository;


    /**
     * 新增监测类型信息，如果想修改，需要传一个id
     * @param payload
     * @return
     */
    public MoitorTypeInfo saveMoitorSiteInfo(Map<String , Object> payload){
        MoitorTypeInfo moitorSiteInfo = null;
        try {
            moitorSiteInfo = (MoitorTypeInfo) MapUtils.convertMap(MoitorTypeInfo.class , payload);
        } catch (IntrospectionException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return moitorSiteInfoRepository.save(moitorSiteInfo);
    }

    /**
     * 查询所有
     * */
    public List<MoitorTypeInfo> getMoitorSiteInfoList() {
        return moitorSiteInfoRepository.findAll();
    }

    /**
     * 没有条件的分页查询
     * */
    public Page<MoitorTypeInfo> findMoitorSiteInfoNoCriteria(Integer page, Integer size) {
        Pageable pageable = new PageRequest(page, size, Sort.Direction.ASC, "createDate");
        return moitorSiteInfoRepository.findAll(pageable);
    }


    /**
     * 按条件分页查询
     * @param page 页数
     * @param size 数量
     * @param moitorSiteInfo 参数
     * @return
     */
    public Page<MoitorTypeInfo> findBookCriteria(Integer page, Integer size, MoitorTypeInfo moitorSiteInfo) {
        //规格定义//页码：前端从1开始，jpa从0开始，做个转换
        Pageable pageable = new PageRequest(page-1,size , Sort.Direction.ASC, "createDate");
        Page<MoitorTypeInfo> moitorSiteInfoPage = moitorSiteInfoRepository.findAll(new Specification<MoitorTypeInfo>(){
            @Override
            public Predicate toPredicate(Root<MoitorTypeInfo> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                Predicate p1 = criteriaBuilder.equal(root.get("areaCode").as(String.class), moitorSiteInfo.getAreaCode());
                query.where(criteriaBuilder.or(p1));
                return query.getRestriction();
            }
        },pageable);
        return moitorSiteInfoPage;
    }

    /**
     * 根据id查询监测类型信息
     * */
    public MoitorTypeInfo findEmissionSourceInfoById(String id) {

        return moitorSiteInfoRepository.findMoitorSiteInfoById(id);
    }

    /**
     * 根据id删除监测类型信息
     * */
    public Boolean delete(String id) {

        Boolean result = false;
        try{
            Integer r = moitorSiteInfoRepository.deleteMoitorSiteInfoById(id);
            if(r == 1){
                result = true;
            }
        }catch (Exception e){
            log.error(e.getLocalizedMessage());
        }
        return result;
    }


}
